package com.kdg.thread.consurrenttest;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kdg
 * @description:
 * @date 2018/5/13.
 */
public class ConditonTest implements Runnable{
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition condition = lock.newCondition();


    @Override
    public void run() {
        lock.lock();
        try {
            System.out.println("before await");
            condition.await();
            System.out.println("going on");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if(lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ConditonTest conditonTest = new ConditonTest();
        Thread thread = new Thread(conditonTest);
        thread.start();
        Thread.sleep(2000);
        System.out.println("before signal");
        lock.lock();
        System.out.println(123456);
        condition.signal();
        lock.unlock();
    }
}
